#include "StdAfx.h"
#include "stdio.h"
#include "xhRayCtrl.h"


#define RayPlug_Dir "PlugRay"
#define RayPlug_Ext "ray"

xhRayCtrl::xhRayCtrl(void)
{
	m_hPlugDll = NULL;
	m_pPlug    = NULL;

	m_hWnd           = NULL;
	m_funMsgCallBack = NULL;
	m_lContext       = 0;
	m_pContext       = NULL;

	m_nDllFileCount = 0;
	memset(m_arDllFileName,0,sizeof(m_arDllFileName));
	memset(m_szUsePlugDesc,0,sizeof(m_szUsePlugDesc));

	memset(m_xBuffer,0,sizeof(m_xBuffer));

	m_nSendFreq     = 0;    
	m_nIntervalTime = 0;

}

xhRayCtrl::~xhRayCtrl(void)
{
	UnInitialize();
}

int xhRayCtrl::Initialize()
{
	DD_AUTO_LOCK xLock(&m_Lock);

	MySafeDelete( m_pPlug );
	MySafeFreeLib( m_hPlugDll );

	m_nDllFileCount = 0;
	memset(m_arDllFileName,0,sizeof(m_arDllFileName));
	memset(&m_xPlugDescs,0,sizeof(m_xPlugDescs));


	memset(m_szUsePlugDesc,0,sizeof(m_szUsePlugDesc));

	LoadType();

	return 0;
}

int xhRayCtrl::UnInitialize()
{
	DDThread4::DDThdUnInit();

	DD_AUTO_LOCK xLock(&m_Lock);

	MySafeDelete( m_pPlug );
	MySafeFreeLib( m_hPlugDll );

	return 0;
}

int xhRayCtrl::LoadType()
{
	char szModulePath[MAX_PATH] = "";
	char szPlugDir[MAX_PATH] = "";
	char szPlugFile[MAX_PATH] = "";

	GetModuleFileName(NULL,szModulePath,MAX_PATH);
	char* sTemp = strrchr(szModulePath,'\\'); 
	sTemp++;
	sTemp[0] = '\0'; 

	sprintf(szPlugDir,"%s%s\\*.%s",szModulePath,RayPlug_Dir,RayPlug_Ext);

	char szCurDir[MAX_PATH] = "";
	sprintf(m_szPlugDir,"%s%s\\",szModulePath,RayPlug_Dir);

	::GetCurrentDirectory( MAX_PATH,szCurDir );
	::SetCurrentDirectory( m_szPlugDir );

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szPlugDir,&filedata); 

	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( filedata.cFileName ) 
			{ 
				sprintf(szPlugFile,"%s%s\\%s",szModulePath,RayPlug_Dir,filedata.cFileName);

				if( 0 == LoadPlug( szPlugFile ) )
				{
					sprintf( m_xPlugDescs.arPlugDescs[m_xPlugDescs.nPlugDescs],filedata.cFileName );
					m_xPlugDescs.nPlugDescs++;

					sprintf( m_arDllFileName[m_nDllFileCount],szPlugFile);
					m_nDllFileCount++;
				}
			} 

		}while(FindNextFile(filehandle,&filedata)); 

		FindClose(filehandle); 
	} 

	::SetCurrentDirectory( szCurDir );

	MySafeDelete( m_pPlug );
	MySafeFreeLib( m_hPlugDll );

	if( m_nDllFileCount > 0 )
	{
		return 0;
	}

	return -1;
}

int xhRayCtrl::GetPlugList(zkPlugDescs **ppDescs)
{	
	*ppDescs = &m_xPlugDescs;

	return 0;
}



int xhRayCtrl::SelectPlug( char *pDescName )
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == pDescName )
	{
		MySafeDelete( m_pPlug );
		return DD_ERR_FAILURE;
	}

	int nRet = DD_ERR_FAILURE;

	if( strcmp(m_szUsePlugDesc,pDescName) == 0 )
	{
		return DD_ERR_SUCCESS;
	}

	DDThread4::DDThdUnInit();

	char szCurDir[MAX_PATH] = "";
	::GetCurrentDirectory( MAX_PATH,szCurDir );
	::SetCurrentDirectory( m_szPlugDir );

	for(int i=0;i<m_xPlugDescs.nPlugDescs;i++)
	{
		if( strcmp(m_xPlugDescs.arPlugDescs[i],pDescName) == 0 )
		{
			if( DD_ERR_SUCCESS == LoadPlug( m_arDllFileName[i] ) )
			{
				nRet = InitPlug();
				if( DD_ERR_SUCCESS == nRet )
				{
					strcpy(m_szUsePlugDesc,pDescName);
				}

				break;
			}
		}
	}

	::SetCurrentDirectory( szCurDir );

	if( DD_ERR_SUCCESS != nRet )
	{
		MySafeDelete( m_pPlug );
		MySafeFreeLib( m_hPlugDll );
	}

	return nRet;
}



int xhRayCtrl::LoadPlug( char *pFileName )
{
	int nRet = -1;

	MySafeDelete( m_pPlug );
	MySafeFreeLib( m_hPlugDll );

	do 
	{
		m_hPlugDll = LoadLibrary( pFileName );
		if(NULL == m_hPlugDll)
		{
			DWORD dwError = GetLastError();
			break;
		}

		LPOPCREATEPLUGMAN CreatePlugMan = (LPOPCREATEPLUGMAN)GetProcAddress(m_hPlugDll, "CreatePlugMan");	
		if( NULL == CreatePlugMan )
		{
			break;
		}

		if( DD_ERR_SUCCESS != CreatePlugMan( &m_pPlug ) )
		{
			MySafeFreeLib( m_hPlugDll );

			break;
		}

		return 0;

	}while( FALSE );

	MySafeDelete( m_pPlug );
	MySafeFreeLib( m_hPlugDll );

	return nRet;
}


int xhRayCtrl::InitPlug()
{
	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}
	m_pPlug->Initialize();

	DDThread4::DDThdInit();
	DDThread4::SetRunEvent2();

	return 0;
}



int xhRayCtrl::SetHwnd(HWND hWnd)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	m_hWnd = hWnd;

	return 0;
}

int xhRayCtrl::RegMsgCallBack(void *pfun, DWORD dwContext, void *pContext)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	m_funMsgCallBack = (RayMsgCallBackFun)pfun;
	m_lContext       = dwContext;
	m_pContext       = pContext;

	return 0;
}

int xhRayCtrl::OpParam(VOID *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pPlug->OpParam(pVoid);
}

int xhRayCtrl::SetComCfg(zkComCfg *pCfg)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	m_nSendFreq     = pCfg->nSendFreq;    
	m_nIntervalTime = 0;

	return m_pPlug->SetComCfg(pCfg);
}

int xhRayCtrl::SetRayCfg(zkRayCfg *pCfg)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pPlug->SetRayCfg(pCfg);
}

int xhRayCtrl::SetRayCtrl(zkRayCtrl *pRayCtrl,BOOL bWaitToGo)
{
	if( NULL == pRayCtrl )
	{
        return DD_ERR_PARAMETER;
	}

	m_nIntervalTime = pRayCtrl->nOpenHighV > 0 ? pRayCtrl->nIntervalTime:0;
	m_nIntervalTime *= 1000;
	bool bEvent = m_nIntervalTime > 0 ? true:false;
	DDThread4::SetRunEvent( bEvent );
	if( bEvent )
	{
		DDThread4::SetRunEvent2( bEvent );
	}
	m_xRayCtrl = *pRayCtrl;

	return SetRayCtrl_2(pRayCtrl,bWaitToGo);
}

int xhRayCtrl::SetRayCtrl_2(zkRayCtrl *pRayCtrl,BOOL bWaitToGo)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	int nRet = 0;
	do 
	{
		nRet = m_pPlug->SetRayCtrl(pRayCtrl,bWaitToGo);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (FALSE);

	return nRet;
}

int xhRayCtrl::GetRayCtrl(zkRayCtrl **ppRayCtrl)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	int nRet = 0;

	do 
	{
		nRet = m_pPlug->GetRayCtrl(ppRayCtrl);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (FALSE);

	return nRet;
}

int xhRayCtrl::CheckConnect(int &nStatus)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pPlug )
	{
		return DD_ERR_NODEV;
	}

	return m_pPlug->CheckConnect(nStatus);
}


int xhRayCtrl::DDThdDispatch(HANDLE hExitEvent)
{
	if( m_nSendFreq <= 0 || m_nIntervalTime <= 0 )
	{
		DDThread4::SetRunEvent(false);
		return 0;
	}

	DWORD dwWait = WaitForSingleObject(hExitEvent,m_nSendFreq);
	if( WAIT_TIMEOUT == dwWait )
	{
		m_nIntervalTime -= m_nSendFreq;

		SetRayCtrl_2(&m_xRayCtrl);
	}

	return 0;
}

int xhRayCtrl::DDThd2Dispatch(HANDLE hExitEvent)
{
	DWORD dwWait = WaitForSingleObject(hExitEvent,500);
	if( WAIT_TIMEOUT == dwWait )
	{
		zkRayCtrl *pRayCtrl = NULL;
        GetRayCtrl( &pRayCtrl );
		if( NULL != pRayCtrl )
		{
			if( m_nIntervalTime <= 0 && pRayCtrl->nOpenHighV == 0 )
			{
				DDThread4::SetRunEvent( false );
			}

			if(  NULL != m_funMsgCallBack )
			{
				m_funMsgCallBack(0,pRayCtrl,sizeof(zkRayCtrl),m_lContext,m_pContext);
			}
		}
	}

	return 0;
}